package net.liteheaven.testfile;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import java.io.File;
import java.io.FileOutputStream;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.hello).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(v.getContext(), FragmentStackActivity.class));
            }
        });

        writeTest();
    }

    private boolean writeTest(){
        File f1 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() /*+ "/plugin/"*/);
        //File f = getCacheDir();
        boolean exists = f1.exists();
        System.out.println("" + f1.toString() + " exists=" + exists);

        try {
            File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/plugin/writeTest.txt");
            FileOutputStream fs = new FileOutputStream(f);

            String text = "aaaaaaaaaaaaaaaaaaaaaa";
            byte[] bytes = text.getBytes();

            for (int i=0; i<2; ++i){
                fs.write(bytes, 0, bytes.length);
            }
            fs.close();
        }
        catch (Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
